//
//  UIColor+MistKit.m
//  MistKit
//
//  Created by ChenJie on 5/30/20.
//

#import "UIColor+MistKit.h"

#define kDefaultColor [UIColor blackColor]

@implementation UIColor (MistKit)

+ (instancetype)mk_colorWithHex:(NSString *)hexString {
    return [self mk_colorWithHex:hexString alpha:1];
}

+ (instancetype)mk_colorWithHex:(NSString *)hexString
                          alpha:(CGFloat)alpha {
    
    hexString =
    [[hexString stringByTrimmingCharactersInSet:
      [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if (hexString.length < 6) {
        return kDefaultColor;
    }
    
    if ([hexString hasPrefix:@"#"]) {
        hexString = [hexString substringFromIndex:1];
    }
    
    if ([hexString hasPrefix:@"0X"]) {
        hexString = [hexString substringFromIndex:2];
    }
    
    if (hexString.length != 6) {
        return kDefaultColor;
    }
    
    NSUInteger hexNumber = [self hexNumberWithString_m1:hexString];
    
    return [self mk_colorWithHexNumber:hexNumber alpha:alpha];
}

+ (instancetype)mk_colorWithHexNumber:(NSUInteger)hexNumber {
    return [self mk_colorWithHexNumber:hexNumber alpha:1];
}

+ (instancetype)mk_colorWithHexNumber:(NSUInteger)hexNumber
                                alpha:(CGFloat)alpha {
    
    if (hexNumber > 0xFFFFFF) {
        return kDefaultColor;
    }
    
    NSInteger red = (hexNumber >> 16) & 0xFF;
    NSInteger green = (hexNumber >> 8) & 0xFF;
    NSInteger blue  = hexNumber & 0xFF;
    
    return [self mk_colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (instancetype)mk_colorWithRed:(NSInteger)red
                          green:(NSInteger)green
                           blue:(NSInteger)blue {
    return [self mk_colorWithRed:red green:green blue:blue alpha:1];
}

+ (instancetype)mk_colorWithRed:(NSInteger)red
                          green:(NSInteger)green
                           blue:(NSInteger)blue
                          alpha:(CGFloat)alpha {
    return [self colorWithRed:red/255.f
                        green:green/255.f
                         blue:blue/255.f
                        alpha:alpha];
}

+ (instancetype)mk_randomColor {
    return [self mk_randomColorWithAlpha:1];
}

+ (instancetype)mk_randomColorWithAlpha:(CGFloat)alpha {
    return [self mk_colorWithRed:arc4random() % 256
                           green:arc4random() % 256
                            blue:arc4random() % 256
                           alpha:alpha];
}

#pragma mark - Private methods

+ (NSUInteger)hexNumberWithString_m1:(NSString *)hexString {
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned int hexNumber;
    [scanner scanHexInt:&hexNumber];
    return hexNumber;
}

+ (NSUInteger)hexNumberWithString_m2:(NSString *)hexString {
    const char *char_str =
    [hexString cStringUsingEncoding:NSASCIIStringEncoding];
    
    unsigned int hexNumber;
    sscanf(char_str, "%x", &hexNumber);
    return hexNumber;
}

+ (NSUInteger)hexNumberWithString_m3:(NSString *)hexString {
    unsigned long hexNumber = strtoul([hexString UTF8String], 0, 16);
    return hexNumber;
}

@end
